package com.zbkj.front.controller.shop;

import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.shop.EbGroupBuyShop;
import com.zbkj.common.model.shop.EbGroupBuyShopVerificationUser;
import com.zbkj.common.model.user.User;
import com.zbkj.common.request.EbGroupBuyShopRequest;
import com.zbkj.common.response.EbGroupBuyShopResponse;
import com.zbkj.common.response.EbGroupBuyShopVerificationUserResponse;
import com.zbkj.common.response.SearchGroupBuyShopResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.EbGroupBuyShopService;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/front/ebGroupBuyShop")
@Api(tags = "团购商家管理")
public class EbGroupBuyShopController {
    @Resource
    EbGroupBuyShopService ebGroupBuyShopService;
    @Autowired
    UserService userService;
    @Autowired
    MerchantService merchantService;

    /**
     * 增加团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @ApiOperation(value = "增加团购店铺")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<EbGroupBuyShop> add(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        return CommonResult.success(ebGroupBuyShopService.add(ebGroupBuyShopRequest));
    }


    /**
     * 修改团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @ApiOperation(value = "修改团购店铺")
    @PostMapping(value = "/update")
    public CommonResult<EbGroupBuyShop> update(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        return CommonResult.success(ebGroupBuyShopService.update(ebGroupBuyShopRequest));
    }

    /**
     * 修改团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @ApiOperation(value = "修改营业信息")
    @PostMapping(value = "/updateBusiness")
    public CommonResult<EbGroupBuyShop> updateBusiness(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        return CommonResult.success(ebGroupBuyShopService.updateBusiness(ebGroupBuyShopRequest));
    }

    /**
     * 修改团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @ApiOperation(value = "审核团购店铺")
    @PostMapping(value = "/check")
    public CommonResult<EbGroupBuyShop> check(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        return CommonResult.success(ebGroupBuyShopService.check(ebGroupBuyShopRequest));
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询详情")
    @GetMapping(value = "/get/{id}")
    public CommonResult<EbGroupBuyShopResponse> get(@PathVariable("id") Long id) {
        return CommonResult.success(ebGroupBuyShopService.getById(id));
    }

    /**
     * 查询用户店铺数量
     *
     * @return
     */
    @ApiOperation(value = "查询详情")
    @GetMapping(value = "/getCount")
    public CommonResult<Integer> getCount() {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(0);
        }
        return CommonResult.success(ebGroupBuyShopService.getCount(merchant.getId()));
    }

    /**
     * 查询列表
     *
     * @return
     */
    @ApiOperation(value = "查询列表")
    @PostMapping(value = "/getList")
    public CommonResult<List<EbGroupBuyShopResponse>> getList(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(new ArrayList<EbGroupBuyShopResponse>());
        }
        ebGroupBuyShopRequest.setMerId(merchant.getId());
        return CommonResult.success(ebGroupBuyShopService.getList(ebGroupBuyShopRequest));
    }


    @ApiOperation(value = "分页查询列表")
    @PostMapping(value = "/getPage")
    public CommonResult<PageInfo<EbGroupBuyShopResponse>> getPage(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(new PageInfo<EbGroupBuyShopResponse>());
        }
        ebGroupBuyShopRequest.setMerId(merchant.getId());
        return CommonResult.success(ebGroupBuyShopService.getPage(ebGroupBuyShopRequest));
    }

    @ApiOperation(value = "首页分页查询")
    @PostMapping(value = "/shop/getPage")
    public CommonResult<PageInfo<SearchGroupBuyShopResponse>> getShopPage(@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {

        return CommonResult.success(ebGroupBuyShopService.getShopPage(ebGroupBuyShopRequest));
    }

    @ApiOperation(value = "查询店铺详情")
    @PostMapping(value = "/shop/getDetails/{id}")
    public CommonResult<EbGroupBuyShopResponse> getDetails(@PathVariable("id") Long id,@RequestBody EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        ebGroupBuyShopRequest.setId(id);
        return CommonResult.success(ebGroupBuyShopService.getDetails(ebGroupBuyShopRequest));
    }

    @ApiOperation(value = "商家核销员列表")
    @PostMapping(value = "/get/shopVerificationUserList")
    public CommonResult<List<EbGroupBuyShopVerificationUserResponse>> getShopVerificationUserList() {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success();
        }
        return CommonResult.success(ebGroupBuyShopService.  getShopVerificationUserList(merchant.getId()));
    }

    @ApiOperation(value = "查询当前用户核销门店")
    @PostMapping(value = "/getUserShopVerification")
    public CommonResult<List<EbGroupBuyShopVerificationUserResponse>> getUserShopVerification() {
        User user = userService.getInfo();
        return CommonResult.success(ebGroupBuyShopService.getUserShopVerification(user.getId()));
    }

    @ApiOperation(value = "新增店铺核销人员")
    @PostMapping(value = "/addUserShopVerification")
    public CommonResult<EbGroupBuyShopVerificationUser> addUserShopVerification(@RequestBody EbGroupBuyShopVerificationUser ebGroupBuyShopVerificationUser) {

        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success();
        }
        ebGroupBuyShopVerificationUser.setMerId(merchant.getId());
        return CommonResult.success(ebGroupBuyShopService.addUserShopVerification(ebGroupBuyShopVerificationUser));
    }

    @ApiOperation(value = "修改店铺核销人员")
    @PostMapping(value = "/setUserShopVerification")
    public CommonResult<EbGroupBuyShopVerificationUser> setUserShopVerification(@RequestBody EbGroupBuyShopVerificationUser ebGroupBuyShopVerificationUser) {

        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success();
        }
        ebGroupBuyShopVerificationUser.setMerId(merchant.getId());
        return CommonResult.success(ebGroupBuyShopService.setUserShopVerification(ebGroupBuyShopVerificationUser));
    }

    @ApiOperation(value = "删除店铺核销人员")
    @PostMapping(value = "/delUserShopVerification")
    public CommonResult<Boolean> delUserShopVerification(@RequestBody EbGroupBuyShopVerificationUser ebGroupBuyShopVerificationUser) {

        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success();
        }
        ebGroupBuyShopVerificationUser.setMerId(merchant.getId());
        return CommonResult.success(ebGroupBuyShopService.delUserShopVerification(ebGroupBuyShopVerificationUser));
    }
}